package com.hnu.tockmarket.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.hnu.tockmarket.R;
import com.hnu.tockmarket.activity.LoginActivity;
import com.hnu.tockmarket.activity.UserAboutActivity;
import com.hnu.tockmarket.activity.UserAdviceActivity;
import com.hnu.tockmarket.activity.UserChangePasswordActivity;
import com.hnu.tockmarket.activity.UserInfoActivity;

import static android.content.Context.MODE_PRIVATE;

/**
 * 个人中心Fragment界面
 *
 * @author hnulht
 * @since 2021-07-07
 */
public class UserFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = "UserFragment"; // 日志
    private Toolbar toolbar; // 工具栏
    private TextView textViewUserName; // 正在登录用户名
    private ImageButton imageButtonUserDefaultAvatar; // 正在登录用户头像
    private LinearLayout linearLayoutUserInfo;
    private LinearLayout linearLayoutUserChangePassword;
    private LinearLayout linearLayoutUserAdvice;
    private LinearLayout linearLayoutUserShare;
    private LinearLayout linearLayoutUserService;
    private LinearLayout linearLayoutUserAbout;
    private LinearLayout linearLayoutUserExit;
    private Intent intent;

    @Override
    public View initView() {
        Log.i(TAG, "初始化个人中心视图");
        View view = View.inflate(getContext(), R.layout.fragment_user, null);
        // 定位控件
        toolbar = view.findViewById(R.id.user_tb);
        textViewUserName = view.findViewById(R.id.user_name_tv);
        imageButtonUserDefaultAvatar = view.findViewById(R.id.user_default_avatar_ib);
        linearLayoutUserInfo = view.findViewById(R.id.user_info_ll);
        linearLayoutUserChangePassword = view.findViewById(R.id.user_change_password_ll);
        linearLayoutUserAdvice = view.findViewById(R.id.user_advice_ll);
        linearLayoutUserShare = view.findViewById(R.id.user_share_ll);
        linearLayoutUserService = view.findViewById(R.id.user_service_ll);
        linearLayoutUserAbout = view.findViewById(R.id.user_about_ll);
        linearLayoutUserExit = view.findViewById(R.id.user_exit_ll);
        // 添加控件监听点击事件
        textViewUserName.setOnClickListener(this);
        imageButtonUserDefaultAvatar.setOnClickListener(this);
        linearLayoutUserInfo.setOnClickListener(this);
        linearLayoutUserChangePassword.setOnClickListener(this);
        linearLayoutUserAdvice.setOnClickListener(this);
        linearLayoutUserShare.setOnClickListener(this);
        linearLayoutUserService.setOnClickListener(this);
        linearLayoutUserAbout.setOnClickListener(this);
        linearLayoutUserExit.setOnClickListener(this);
        return view;
    }

    @Override
    public void initData() {
        Log.i(TAG, "初始化个人中心数据");
        super.initData();
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login_data", MODE_PRIVATE);
        String name = sharedPreferences.getString("name", "点击登录");
        // 把用户名字显示到textView控件上
        textViewUserName.setText(name);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_name_tv:
            case R.id.user_default_avatar_ib:
                if (textViewUserName.getText().toString().trim().equals("点击登录")) {
                    SharedPreferences.Editor editor = getActivity().getSharedPreferences("login_data", MODE_PRIVATE).edit();
                    editor.clear(); // 清除本地保存的登录用户信息
                    editor.apply();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                } else {
                    intent = new Intent(getContext(), UserInfoActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.user_info_ll:
                if (textViewUserName.getText().toString().trim().equals("点击登录")) {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    intent = new Intent(getContext(), UserInfoActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.user_change_password_ll:
                if (textViewUserName.getText().toString().trim().equals("点击登录")) {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    intent = new Intent(getContext(), UserChangePasswordActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.user_advice_ll:
                if (textViewUserName.getText().toString().trim().equals("点击登录")) {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    intent = new Intent(getContext(), UserAdviceActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.user_share_ll:
                intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
                intent.putExtra(Intent.EXTRA_TEXT, "https://gitee.com/hnulht/stockmarket-app");
                startActivity(Intent.createChooser(intent, "项目代码地址"));
                break;
            case R.id.user_service_ll:
                try {
                    String qqUrl = "mqqwpa://im/chat?chat_type=wpa&uin=381874042&version=1";
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), "请检查是否安装QQ", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.user_about_ll:
                intent = new Intent(getContext(), UserAboutActivity.class);
                startActivity(intent);
                break;
            case R.id.user_exit_ll:
                if (textViewUserName.getText().toString().trim().equals("点击登录")) {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    SharedPreferences.Editor editor = getActivity().getSharedPreferences("login_data", MODE_PRIVATE).edit();
                    editor.clear(); // 清除本地保存的登录用户信息
                    editor.apply();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
                break;
        }
    }

}